// api/order_api/order_remove.go
package order_api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/models/ctype"
	"muziai-server/utils/jwts"
	"muziai-server/utils/res"
)

func (OrderApi) OrderRemoveView(c *gin.Context) {
	var cr models.IDListRequest
	err := c.ShouldBindJSON(&cr)
	if err != nil {
		res.FailWithError(c, err)
		return
	}
	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.Claims)
	query := global.DB.Where("")
	if claims.IsAuth() {
		query.Where("user_id = ?", claims.UserID)
	}

	var orderList []models.OrderModel
	global.DB.Where(query).Find(&orderList, "id in ?", cr.IDList)

	if len(orderList) > 0 {
		for _, order := range orderList {
			if order.PayStatus == ctype.UnpaidPayStatus { // 假设这里是需要检查的状态
				res.FailWithMsg(c, fmt.Sprintf("订单%d:未支付的订单不能删除", order.No))
				return
			}
		}
		if claims.IsAuth() {
			// 逻辑删除
			global.DB.Delete(&orderList)
		} else {
			// 真删除
			global.DB.Unscoped().Delete(&orderList)
		}
	}
	msg := fmt.Sprintf("删除订单%d条，成功删除%d条", len(cr.IDList), len(orderList))

	res.OkWithMsg(c, msg)
}
